<template>
  <div class="coupon-container">
    <div class="header">
      <a href="javascript:void(0)" @click="$router.push({ name: 'PersonalCenter' })" class="back-button">
        <img src="./me-img/back.png" alt="返回" width="40%" height="100%" />
      </a>
      <img src="./me-img/logo.png" alt="logo" class="logo" />
    </div>

    <div class="body">
      <div class="tabs">
        <div v-for="tab in tabs" :key="tab.id" :class="['tab', { active: activeTab === tab.id }]" @click="activeTab = tab.id">
          {{ tab.name }}
        </div>
      </div>

      <div class="coupon-list">
        <div
          v-if="filteredCoupons.length > 0"
          v-for="(coupon, index) in filteredCoupons"
          :key="index"
          class="coupon-item"
          :style="{
            backgroundColor: coupon.visual.background,
            color: coupon.visual.text_color,
            border: coupon.visual.border,
          }"
        >
          <div class="coupon-content">
            <div class="coupon-top">
              <div
                class="discount-badge"
                :style="{
                  color: coupon.visual.discount_badge.color,
                  backgroundColor: coupon.visual.discount_badge.background,
                  fontSize: coupon.visual.discount_badge.font_size,
                }"
              >
                {{ coupon.visual.discount_badge.content }}<span v-if="!coupon.visual.discount_badge.content.includes('%')">￥</span>
              </div>

              <div class="coupon-info">
                <h3 class="coupon-name">{{ coupon.fields.name }}</h3>
                <p class="coupon-highlight">{{ coupon.fields.highlight }}</p>
                <p class="coupon-details">{{ coupon.fields.details }}</p>
                <p class="coupon-validity">
                  有效期: {{ formatDate(coupon.validity.start_time) }} 至
                  {{ formatDate(coupon.validity.end_time) }}
                </p>
              </div>
            </div>

            <div class="coupon-bottom">
              <button
                class="receive-btn"
                :class="{
                  'disabled-btn': activeTab === 'expired' || activeTab === 'received',
                }"
                @click="handleReceive(coupon)"
              >
                {{ getButtonText(coupon) }}
              </button>
            </div>
          </div>
        </div>

        <div v-else class="empty-coupon">
          <img src="./me-img/empty-coupon.png" alt="无优惠券" class="empty-img" />
          <p class="empty-text">暂无{{ activeTab === "available" ? "可领取" : activeTab === "received" ? "已领取" : "已过期" }}的优惠券</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import couponData from "./meCoupon.json";

export default {
  name: "CouponList",
  data() {
    return {
      activeTab: "available", // 默认显示可领取标签页
      tabs: [
        { id: "available", name: "可领取" },
        { id: "received", name: "已领取" },
        { id: "expired", name: "已过期" },
      ],
      coupons: couponData.coupon,
      receivedCoupons: JSON.parse(localStorage.getItem("receivedCoupons")) || [],
    };
  },
  created() {
    // 如果从路由参数中获取到了tab，则使用路由参数的值
    if (this.$route.query.tab) {
      this.activeTab = this.$route.query.tab;
    }
  },
  computed: {
    filteredCoupons() {
      const now = new Date();
      if (this.activeTab === "available") {
        return this.coupons.filter((coupon) => {
          const end = new Date(coupon.validity.end_time);
          return end > now && !this.receivedCoupons.some((rc) => rc.fields.name === coupon.fields.name);
        });
      } else if (this.activeTab === "expired") {
        return this.coupons.filter((coupon) => {
          const end = new Date(coupon.validity.end_time);
          return end <= now;
        });
      } else if (this.activeTab === "received") {
        return this.receivedCoupons;
      }
      return [];
    },
  },
  methods: {
    formatDate(dateString) {
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
    },
    handleReceive(coupon) {
      if (this.activeTab === "available") {
        // 添加到已领取列表
        if (!this.receivedCoupons.some((rc) => rc.fields.name === coupon.fields.name)) {
          this.receivedCoupons.push(coupon);
          localStorage.setItem("receivedCoupons", JSON.stringify(this.receivedCoupons));
          this.$message.success(`成功领取优惠券: ${coupon.fields.name}`);
          // 自动刷新列表
          this.$forceUpdate();
        }
      } else if (this.activeTab === "received") {
        // 使用优惠券逻辑
        this.$message.info(`使用优惠券: ${coupon.fields.name}`);
      }
    },
    getButtonText(coupon) {
      if (this.activeTab === "expired") return "已过期";
      if (this.activeTab === "received") return "立即使用";
      if (this.receivedCoupons.some((rc) => rc.fields.name === coupon.fields.name)) return "已领取";
      return "立即领取";
    },
  },
  watch: {
    "$route.query.tab"(newTab) {
      if (newTab) {
        this.activeTab = newTab;
      }
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

.coupon-container {
  min-height: 100vh;
  background-color: #ffffff;
  min-width: 1000px;
  overflow-x: auto;
}

.header {
  background-color: #f4efd2;
  height: 50px;
  align-items: center;
  box-shadow: #bdbbb7 0px 2px 8px;
  padding: 2px 0px;
}

.header,
.body {
  width: 60%;
  margin: 0px auto;
  overflow: hidden;
  display: flex;
}

.body {
  margin: 30px auto;
  height: 700px;
  background-color: #ffffff;
}

.logo {
  width: 10%;
  height: auto;
  margin-left: auto;
}

.back-button {
  color: #333;
  display: flex;
  width: 10%;
  height: 100%;
  text-decoration: none;
  line-height: 40px;
  overflow: hidden;
}

.tabs {
  width: 10%;
  background-color: #f4efd2;
  margin: 0px 20px 0px 0px;
  box-shadow: #bdbbb7 0px 0px 5px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

.tab {
  flex: 1;
  margin: 20px;
  font-size: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
  border: 1px solid rgb(212, 211, 211);
  width: 35%;
  padding: 15px 0;
  color: black;
  border-radius: 5px;
  writing-mode: vertical-rl;
}

.tab.active {
  background-color: #d9c596;
  box-shadow: #bdbbb7 0px 0px 5px;
}

.coupon-list {
  width: 90%;
  box-shadow: #bdbbb7 0px 0px 5px;
  background-color: #f4efd2;
  padding: 20px 30px;
  overflow-y: auto;
}

.coupon-item {
  width: 100%;
  padding: 15px;
  background-color: white;
  border-radius: 8px;
  display: flex;
  box-shadow: #bdbbb7 0px 0px 3px;
  margin: 10px 0px;
}

.coupon-content {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.coupon-top {
  display: flex;
  padding: 15px;
  align-items: center;
}

.discount-badge {
  font-weight: bold;
  padding: 10px;
  border-radius: 8px;
  min-width: 70px;
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-right: 15px;
  flex-shrink: 0;
}

.coupon-info {
  flex: 1;
}

.coupon-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
}

.coupon-highlight {
  color: #ff6b6b;
  font-size: 13px;
  margin-bottom: 3px;
}

.coupon-details {
  font-size: 12px;
  color: #666;
  margin-bottom: 3px;
}

.coupon-validity {
  font-size: 12px;
  color: #999;
}

.coupon-bottom {
  padding: 10px 15px;
  border-top: 1px dashed #e0e0e0;
  text-align: right;
}

.receive-btn {
  background-color: #ff6b6b;
  color: white;
  border: none;
  padding: 8px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.receive-btn:hover {
  background-color: #ff5252;
}

.disabled-btn {
  background-color: #cccccc;
  cursor: not-allowed;
}

.disabled-btn:hover {
  background-color: #cccccc;
}

.empty-coupon {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 50px 0;
}

.empty-img {
  width: 120px;
  height: 120px;
  margin-bottom: 20px;
}

.empty-text {
  color: #999;
  font-size: 16px;
}
</style>
